package top.yangyitao.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import top.yangyitao.dto.PaySlipDto;
import top.yangyitao.dto.ResultDto;
import top.yangyitao.dto.TableDto;
import top.yangyitao.dto.UserDto;
import top.yangyitao.entity.Organization;
import top.yangyitao.entity.PaySlip;
import top.yangyitao.entity.User;
import top.yangyitao.service.PaySlipService;
import top.yangyitao.service.UserService;

@RestController
public class OrganizationPaySlipController {

	@Autowired
	PaySlipService organizationPaySlipService;
	@Autowired
	UserService userService;
	
	@RequiresPermissions("organizationPaySlip_html")
	@RequestMapping("/organizationPaySlip_html")
	public ModelAndView organizationPaySlip_html() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("/organizationPaySlip.html");
		return mv;
	}
	
	@RequiresPermissions("organizationPaySlip_html")
	@RequestMapping("/addOrganizationPaySlip")
	public ResultDto addPaySlip(String printDay,String startDay,String endDay,int workDays,int lateOrLeaveTimes,
			int absentTimes,String realCash,String cash,String bonusCash,String username) {
		User user = userService.findByUsername(username);
		if(user!=null) {
			UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
			User orgAdmin = userService.getOne(userDto.getId());
			Organization organization = orgAdmin.getOrganization();
			if(user.getOrganization().getId()!=organization.getId())
				return new ResultDto("非本机构人员",null,2);
			PaySlip organizationPaySlip = new PaySlip();
			organizationPaySlip.setPrintDay(printDay);
			organizationPaySlip.setStartDay(startDay);
			organizationPaySlip.setEndDay(endDay);
			organizationPaySlip.setWorkDays(workDays);
			organizationPaySlip.setLateOrLeaveTimes(lateOrLeaveTimes);
			organizationPaySlip.setAbsentTimes(absentTimes);
			organizationPaySlip.setRealCash(realCash);
			organizationPaySlip.setCash(cash);
			organizationPaySlip.setBonusCash(bonusCash);
			organizationPaySlip.setUser(user);
			organizationPaySlipService.save(organizationPaySlip);
			return new ResultDto("添加工资条成功",null,0);
		}
		else {
			return new ResultDto("用户名不存在",null,2);
		}
	}

	
	@RequiresPermissions("organizationPaySlip_html")
	@RequestMapping("/getOrganizationPaySlipPage")
	public TableDto<PaySlipDto> getPaySlipPage(Integer pageNumber,Integer pageSize,String startDay,String endDay,String search) {
		UserDto userDto = (UserDto) SecurityUtils.getSubject().getSession().getAttribute("userDto");
		User orgAdmin = userService.getOne(userDto.getId());
		Organization organization = orgAdmin.getOrganization();
		Page<PaySlip> pageResult = organizationPaySlipService.getPaySlipPage(pageNumber,pageSize,organization.getId(),null,startDay,endDay,search);
		List<PaySlip> organizationPaySlips = pageResult.getContent();
		List<PaySlipDto> organizationPaySlipDtos = new ArrayList<PaySlipDto>();
		organizationPaySlips.forEach(organizationPaySlip->organizationPaySlipDtos.add(new PaySlipDto().convert(organizationPaySlip)));
		return new TableDto<PaySlipDto>(organizationPaySlipDtos,pageResult.getTotalElements());
	}
}
